package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.ItemPageService;
import com.dongdongshop.service.TbGoodsDescService;
import com.dongdongshop.service.TbGoogsService;
import com.dongdongshop.service.itemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemPageServiceImpl implements ItemPageService {
    
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Reference
    private TbGoogsService tbGoogsService;

    @Reference
    private TbGoodsDescService tbGoodsDescService;

    @Reference
    private itemService service;
    
    @Override
    public void itemPage(Long goodsId) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter = null;
        try {
            Template template = configuration.getTemplate("item.ftl");
            Map map = new HashMap<>();

            TbGoods tbGoods = tbGoogsService.selectByid(goodsId);
            TbGoodsDesc tbGoodsDesc = tbGoodsDescService.selectByid(goodsId);
            List<TbItem> itemList = service.itemListById(goodsId);

            map.put("tbGoods",tbGoods);
            map.put("tbGoodsDesc",tbGoodsDesc);
            map.put("itemList",itemList);


            fileWriter = new FileWriter("F:\\item_page\\"+goodsId+".html");
            template.process(map,fileWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(fileWriter != null){
                    fileWriter.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
